package leetcode.每日一题;

import leetcode.util.CommonUtils;
import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/3/24 11:56
 * @description：https://leetcode-cn.com/problems/image-smoother/
 */
public class 图片平滑器 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(2 / 3);
        CommonUtils.arrTransfer("[[100,200,100],[200,50,200],[100,200,100]]");
        int[][] ans = solution.imageSmoother(new int[][]{{100, 200, 100}, {200, 50, 200}, {100, 200, 100}});

    }

    class Solution {
        int[][] dirs = new int[][]{{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};

        public int[][] imageSmoother(int[][] img) {
            int m = img.length;
            int n = img[0].length;
            int[][] ans = new int[m][n];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    int sum = img[i][j];
                    int count = 1;
                    for (int[] dir : dirs) {
                        int x = i + dir[0];
                        int y = j + dir[1];
                        if (x >= 0 && x < m && y >= 0 && y < n) {
                            sum += img[x][y];
                            count++;
                        }
                    }
                    ans[i][j] = sum / count;
                }
            }
            return ans;
        }
    }
}
